package br.com.JZSolution.domineduplex.gui.preferencias;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.com.JZSolution.domineduplex.DomineDuplex;
import br.com.JZSolution.domineduplex.rede.ControladorDeServidor;
import br.com.JZSolution.domineduplex.utils.ChecadorDePorta;
import br.com.JZSolution.domineduplex.utils.FiltroPasta;

public class PainelDeRecepcaoPreferencias extends JPanel {
	
	JFileChooser escolherArquivo;
	File arquivoParaEnviar;

	private JTextField campoPastaBase;
	private JTextField campoPortaServer;
	private GridBagConstraints c = new GridBagConstraints();
	private JPanel painelCentral ;
	private boolean reconectar = false;
	private JLabel mensagemTestePorta;
	
	public PainelDeRecepcaoPreferencias(final JDialog componentePai, final String porta, final String pasta){
		super(new FlowLayout(FlowLayout.LEFT));
		
		painelCentral = new JPanel(new GridBagLayout());
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(3,0,3,0);
		
		JLabel arquivo = new JLabel("Salvar em:  ");
		
		campoPastaBase = new JTextField(pasta);
		campoPastaBase.setPreferredSize(new Dimension(300,30));
		
		JButton btnArquivo = new JButton("...");
		btnArquivo.setPreferredSize(new Dimension(25,30));
		
		btnArquivo.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
					escolherArquivo = new JFileChooser();
					escolherArquivo.addChoosableFileFilter(new FiltroPasta());
					escolherArquivo.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					
			        int returnVal = escolherArquivo.showDialog(componentePai, "Selecionar");
			        
			        if (returnVal == JFileChooser.APPROVE_OPTION) {
			        	arquivoParaEnviar = escolherArquivo.getSelectedFile();
			        	campoPastaBase.setText(arquivoParaEnviar.getAbsolutePath());
			        } 
			}
		});
		
		JLabel portaLabel = new JLabel("Porta: ");
		campoPortaServer = new JTextField(porta);
		campoPortaServer.setPreferredSize(new Dimension(60,30));
		
		painelCentral.add(arquivo,c);
		c.gridx = 1;
		painelCentral.add(campoPastaBase,c);
		c.gridx = 2;
		painelCentral.add(btnArquivo,c);
		
		c.gridx = 0;
		c.gridy = 1;
		painelCentral.add(portaLabel,c);
		c.gridx = 1;
		painelCentral.add(campoPortaServer,c);
		
		c.gridx = 2;
		JButton testarPorta = new JButton("Testar");
		testarPorta.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(ControladorDeServidor.isConectado()){
					int resposta = JOptionPane.showConfirmDialog(componentePai, "O teste irá desconectar o programa e conectará novamente, deseja continuar?", "Teste de disponibilidade de porta", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
					if(resposta == JOptionPane.YES_OPTION){
						ControladorDeServidor.desconectar();
						reconectar = true;
					}else{
						return;
					}
				}
				
				Thread t = new Thread(){
					public void run() {
						if(ChecadorDePorta.checar(campoPortaServer.getText()))
							mensagemTestePorta.setText("Porta "+campoPortaServer.getText()+" aberta para transferências!");
						else	
							mensagemTestePorta.setText("Porta "+campoPortaServer.getText()+" bloqueada, utilize outra porta!");
						painelCentral.invalidate();
						if(reconectar){
							try{
								ControladorDeServidor.Conectar();
							}catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				};
				t.start();
			}
		});
		painelCentral.add(testarPorta,c);
		
		c.gridx = 1;
		c.gridy = 2;
		mensagemTestePorta = new JLabel();
		painelCentral.add(mensagemTestePorta,c);
		
		this.add(painelCentral);
	}
		
	public JTextField getCampoPastaBase() {
		return campoPastaBase;
	}
	public JTextField getCampoPortaServer() {
		return campoPortaServer;
	}
	
	public void setCampoPastaBase(JTextField campoPastaBase) {
		this.campoPastaBase = campoPastaBase;
	}
	public void setCampoPortaServer(JTextField campoPortaServer) {
		this.campoPortaServer = campoPortaServer;
	}
}
